//
//  LFAnimateView.m
//  测试多图动画
//
//  Created by LF on 2017/3/4.
//  Copyright © 2017年 GG. All rights reserved.
//

#import "LFAnimateView.h"


@interface LFAnimateImageView : UIImageView

+ (instancetype) shareInstance;

@end

@implementation LFAnimateImageView

static LFAnimateImageView *animateImage;

+ (instancetype) shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        animateImage = [[LFAnimateImageView alloc]initWithImage:[UIImage imageNamed:@"icon_loading_loading"]];
        animateImage.animationImages = @[[UIImage imageNamed:@"icon_loading_loading"],[UIImage imageNamed:@"icon_loading_loading"]];
        animateImage.animationDuration = 0.5;
        animateImage.animationRepeatCount = LONG_MAX;
        animateImage.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        animateImage.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
    });
    
    return animateImage;
}

@end


@implementation LFAnimateView


+ (instancetype) sharedInstance
{
    static LFAnimateView *animateView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        animateView = [[LFAnimateView alloc] init];
        animateView.backgroundColor = [UIColor whiteColor];
    });
    
    return animateView;
}

+ (void) showInfoView:(UIView *)superView
{
    LFAnimateView *animateView = [LFAnimateView sharedInstance];
    animateView.frame = CGRectMake(0, 0, superView.size.width, superView.size.height);
    [superView addSubview:animateView];
    
    // 先删了所有子视图
    [animateView lf_removeAllSubViews];
    
    // 加上图片
    LFAnimateImageView *animateImage = [LFAnimateImageView shareInstance];
    animateImage.image = [UIImage imageNamed:@"icon_loading_loading"];
    animateImage.center = animateView.center;
    [animateImage startAnimating];
    
    // 加上label
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, animateImage.bottom + 10, superView.size.width, 21)];
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"努力加载中..";
    [label setFont:[UIFont fontWithName:@"Zapfino" size:18]];
    
    [animateView addSubview:animateImage];
    [animateView addSubview:label];
}


+ (void) hideType:(HideType)hideType
{
    LFAnimateImageView *animateImage = [LFAnimateImageView shareInstance];
    
    UIImage *lastImage;
    NSString *lastName;
    CGFloat afterTime = 0.0;
    
    switch (hideType) {
        case 0:
        {
            lastName = @"icon_loading_loading";
            afterTime = 0;
        }
            break;
        case 1:
        {
            lastName = @"img_loading_success";
        }
            break;
        case 2:
        {
            lastName = @"icon_loading_failed";
            afterTime = 0.5;
        }
            break;
            
        default:
            break;
    }
    
    [animateImage stopAnimating];
    
    lastImage = [UIImage imageNamed:lastName];
    
    animateImage.image = lastImage;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(afterTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //[animateImage removeFromSuperview];
        [[LFAnimateView sharedInstance] removeFromSuperview];
    });
}

@end
